<?php
class BankAccounts extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organizations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'bank_accounts';
	}
	
	public function rules()
	{
		return array(
			array('bank_name, branch_name', 'length', 'max'=>45),
			array('city', 'length', 'max'=>30),
			array('zip', 'length', 'max'=>5),
			array('routing_number', 'length', 'max'=>9),
			
		);
	}
	
	public function relations()
	{
		return array(
			'organization'=>array(self::BELONGS_TO, 'Organizations', 'organization_ID'),
		);
	}

	public function getAccount($userID) {
		
		$userInfo = Users::model()->getUser($userID);
		
		foreach ($userInfo as $record) {
			$organizationID = $record->organization_ID;
		}
		
		$sql = "SELECT account_ID, bank_name, branch_name, city, state, zip, routing_number FROM ".$this->tableName()." WHERE organization_ID='".$organizationID."'";
		$resultset = $this->findAllBySql($sql);
		
		return $resultset;
	}
	
	public function getLastFourDigits($userID) {
		
		$userInfo = Users::model()->getUser($userID);
		
		foreach ($userInfo as $record) {
			$organizationID = $record->organization_ID;
		}
		
		$sql = "SELECT four_digit_account_number FROM ".$this->tableName()." WHERE organization_ID='".$organizationID."'";
		$resultset = $this->findAllBySql($sql);
		
		foreach ($resultset as $row) {
			$lastFourDigits = $row->four_digit_account_number;
		}
		
		return $lastFourDigits;
	}
}
?>